#include<iostream>
#include<vector>

using namespace std;


class Solution {
public:
    vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k)
    {
        int n = mat.size(), m = mat[0].size();
        vector<vector<int>> dp(n + 1, vector<int>(m + 1));
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + mat[i - 1][j - 1] - dp[i - 1][j - 1];

            }
        }
        vector<vector<int>> ret(n, vector<int>(m));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                int x1 = max(i + 1 - k, 1), x2 = min(i + 1 + k, n), y1 = max(1, j + 1 - k), y2 = min(m, j + 1 + k);
                ret[i][j] = dp[x2][y2] - dp[x1 - 1][y2] - dp[x2][y1 - 1] + dp[x1 - 1][y1 - 1];

            }
        }

        return ret;

    }
};


int main()
{
    vector<vector<int>> vt = { {1,2,3} ,{4,5,6},{7,8,9} };
    class Solution().matrixBlockSum(vt, 1);


    return 0;
}